package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.commodity.models.vo.CommodityPriceVO;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgTopicMapper;
import com.ztgf.shopping.models.bo.epg.EpgTopicBO;
import com.ztgf.shopping.models.bo.epg.EpgTopicSaveBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.epg.EpgTopicDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.epg.IEpgTopicService;
import com.ztgf.shopping.models.vo.epg.EpgTopicVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: chenyu.wang
 * date: 2020-05-20 14:18
 * Description:
 */
@DubboService
public class EpgTopicServiceImpl extends ServiceImpl<EpgTopicMapper, EpgTopicDTO> implements IEpgTopicService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgTopicMapper epgTopicMapper;

    @DubboReference
    private ICommodityInfoService iCommodityInfoService;


    @Override
    public boolean add(EpgTopicSaveBO topicSaveBO) {
        EpgTopicDTO epgTopicDTO = new EpgTopicDTO();
        BeanUtils.copyProperties(topicSaveBO, epgTopicDTO);
        epgTopicDTO.setId(ymlConfig.getPkId());
        return this.insert(epgTopicDTO);
    }

    /**
     * 删除功能功能相关的推荐
     * @return
     */
    public boolean deleteByModuleId(Long moduleId){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("module_id",moduleId);
        return epgTopicMapper.deleteByMap(columnMap) > 0;
    }

    /**
     * 根据条件查询 推荐专区信息 有页码
     * @param epgTopicBO
     * @return
     */
    @Override
    public Page<EpgTopicVO> getBySelectTopicInfoPage(EpgTopicBO epgTopicBO){
        epgTopicBO = addMustData(epgTopicBO);
        Wrapper<EpgTopicDTO> wrapper = getBySelectTopicInfo(epgTopicBO);
        Page epgTopicDTOPage = selectPage(new Page<>(epgTopicBO.getPageNo(),epgTopicBO.getPageSize()),wrapper);

        List<EpgTopicVO> epgTopicVOList = new ArrayList<>();
        for (Object epgTopicDTOTemp : epgTopicDTOPage.getRecords()) {
            EpgTopicVO epgTopicVO = new EpgTopicVO();
            BeanUtils.copyProperties(epgTopicDTOTemp, epgTopicVO);
            CommodityPriceVO commodityPriceVO = iCommodityInfoService.getByIdOtherInterface(epgTopicVO.getCommodityId());
            epgTopicVO.setCommodityPriceVO(commodityPriceVO);
            epgTopicVOList.add(epgTopicVO);
        }
        return epgTopicDTOPage.setRecords(epgTopicVOList);
    }

    /**
     * 根据条件查询 推荐专区信息 无页码
     * @param epgTopicBO
     * @return
     */
    @Override
    public List<EpgTopicVO> getBySelectTopicInfoNoPage(EpgTopicBO epgTopicBO){
        epgTopicBO = addMustData(epgTopicBO);
        Wrapper<EpgTopicDTO> wrapper = getBySelectTopicInfo(epgTopicBO);
        List<EpgTopicDTO> epgTopicDTOList = null;

        if(epgTopicBO.getPageNo() == 0 && epgTopicBO.getPageSize() == 0)
            epgTopicDTOList =  epgTopicMapper.selectPage( new Page<>(0, ShoppingConstant.TOPIC),wrapper);
        else{
            epgTopicDTOList =  selectList(wrapper);
        }

        List<EpgTopicVO> epgTopicVOList = new ArrayList<>();
        for (EpgTopicDTO epgTopicDTOTemp : epgTopicDTOList) {
            EpgTopicVO epgTopicVO = new EpgTopicVO();
            BeanUtils.copyProperties(epgTopicDTOTemp, epgTopicVO);
            CommodityPriceVO commodityPriceVO = iCommodityInfoService.getByIdOtherInterface(epgTopicVO.getCommodityId());
            epgTopicVO.setCommodityPriceVO(commodityPriceVO);
            epgTopicVO.setSkipUrl(epgTopicDTOTemp.getSkipUrlPc());
            epgTopicVOList.add(epgTopicVO);
        }
        return epgTopicVOList;
    }

    /**
     * 添加必要数据
     * @param epgTopicBO
     * @return
     */
    private EpgTopicBO addMustData(EpgTopicBO epgTopicBO){
        epgTopicBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgTopicBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
        epgTopicBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgTopicBO;
    }

    private Wrapper<EpgTopicDTO> getBySelectTopicInfo(EpgTopicBO epgTopicBO){
        Wrapper<EpgTopicDTO> wrapper = new EntityWrapper<>();
        EpgTopicDTO epgTopicDTO = new EpgTopicDTO();
        BeanUtils.copyProperties(epgTopicBO, epgTopicDTO);
        wrapper.eq("status", epgTopicBO.getStatus());
        wrapper.eq("is_publish", epgTopicBO.getIsPublish());
        wrapper.eq("module_id", epgTopicBO.getModuleId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgTopicBO.getSort().intValue())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }
}
